package com.adong.service.impl;

import com.adong.dao.RoleDao;
import com.adong.dao.UserDao;
import com.adong.domain.Role;
import com.adong.domain.User;
import com.adong.service.UserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author com.adong
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;

    }

    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;

    }


    @Override
    public List<User> list() {
        List<User> userList = userDao.finAll();
        //封装每一个User中role对象
        for (User user : userList) {
            Long id = user.getId();
            List<Role> roles = roleDao.findRoleByUserId(id);
            //将role集合封装到user中取
            user.setRoles(roles);
        }
        System.out.println(userList);
        return userList;

    }

    @Transactional
    @Override
    public void save(User user, long[] roleIds) {
        //user表中存数据
        long userId = userDao.save(user);

        userDao.saveUSerRole(userId,roleIds);
    }

    @Transactional
    @Override
    public void delUser(long userId) {
        userDao.delUserRole(userId);
        int x = 10/0 ;
        userDao.del(userId);
    }
}
